﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    saveImageMutex = new QMutex();
    imageZoomMutex = new QMutex();
    //初始化
    initVar();
    initConf();
    initCameraTimer();
    initCamera();
    initScreen();
}

MainWindow::~MainWindow()
{
    delete camera;
    delete cameraTimer;
    delete conf;
    delete colorWindow;
    delete enlargeWindow;
    delete imageZoomMutex;
    delete saveImageMutex;
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    retInfo("closeEvent");
    if(cameraStatus){
        camera->release();
    }
    cameraTimer->stop();
    event->accept();
}

void MainWindow::initScreen(void){
    retInfo("initScreen");
    //屏幕
    setWindowTitle(QString("%0").arg(conf->confVar.Title));
    setWindowFlags (Qt::FramelessWindowHint);
    setWindowState(Qt::WindowFullScreen);
    // showFullScreen();
    //ZOOM
    QObject::connect(ui->labelImage, SIGNAL(imageDoubleClicked(void)), this, SLOT(recvImageDoubleClicked(void)));
    QObject::connect(ui->labelImage, SIGNAL(imageZoom(int,int,int)), this, SLOT(recvImageZoom(int,int,int)));
    QObject::connect(ui->labelImage, SIGNAL(imageMove(int,int)), this, SLOT(recvImageMove(int,int)));
    //局部放大
    QObject::connect(enlargeWindow, SIGNAL(sendExit(void)), this, SLOT(onImageEnlarge(void)));
    QObject::connect(enlargeWindow, SIGNAL(sendZoom(float)), this, SLOT(recvEnclargeZoom(float)));
    QObject::connect(enlargeWindow, SIGNAL(sendMove(int,int)), this, SLOT(recvEnclargeMove(int,int)));
    QObject::connect(enlargeWindow, SIGNAL(sendSize(int,int)), this, SLOT(recvEnclargeSize(int,int)));
    QObject::connect(this, SIGNAL(SendEnclargeImage(void)), enlargeWindow, SLOT(flushImage()));
    QObject::connect(this, SIGNAL(SendEnclargeCursorPos(int,int)), enlargeWindow, SLOT(recvImageCursorPos(int,int)));
    QObject::connect(this, SIGNAL(SendEnclargeDouble(void)), enlargeWindow, SLOT(recvImageDoubleClicked(void)));
    enlargeWindow->hide();
    //颜色设置
    QObject::connect(colorWindow, SIGNAL(sendExit(void)), this, SLOT(onImageColor(void)));
    QObject::connect(colorWindow, SIGNAL(sendContrast(int)), this, SLOT(recvContrast(int)));
    QObject::connect(colorWindow, SIGNAL(sendBrightness(int)), this, SLOT(recvBrightness(int)));
    QObject::connect(colorWindow, SIGNAL(sendSaturation(int)), this, SLOT(recvSaturation(int)));
    QObject::connect(colorWindow, SIGNAL(sendGamma(int)), this, SLOT(recvGamma(int)));
    colorWindow->hide();
}

void MainWindow::initVar(void){
    // isLeftMousePressMove = false;

    cameraMaxIndex = -1;
    enlargeWindow = new enlargeUI();
    colorWindow = new colorUI();
    bImageEnlarge = false;
    bImageColor = false;
    curDir = "";
    curFileNo = 0;
    imagePosX=0;
    imagePosY=0;
    imageBoxX=0;
    imageBoxY=0;
    imageMoveX=0;
    imageMoveY=0;
    imageZoom=0;
    isFlushZoom = false;
}

void MainWindow::initConf(void){
    retInfo("initConf");
    conf = new Config();
    conf->setConfName(QString("%0/vcp.ini").arg(getCurPath()));
    if(!conf->readConfig()){
        QMessageBox::warning(this,
                             "提示",
                             "配置文件参数错误，请检查");
        close();
    }
}

void MainWindow::initCameraTimer(void){
    cameraTimer = new QTimer(this);
    connect(cameraTimer, SIGNAL(timeout()), this, SLOT(flushCameraTimer()));
}

void MainWindow::changeCamera(void){
    VideoCapture *cap = new VideoCapture;
    int index = conf->confVar.CameraIndex + 1;
    bool ret = false;
    while(true){
        retInfo(QString("changeCamera %0 %1").arg(index).arg(cameraMaxIndex));
        if(cameraMaxIndex>=0 && index > cameraMaxIndex){
            index = 0;
        }
        if(cap->open(index, CAP_DSHOW)){
            cap->release();
            ret = true;
            break;
        }
        cameraMaxIndex = conf->confVar.CameraIndex;
        if(cameraMaxIndex == 0){
            break;
        }
        index++;
    }
    delete cap;
    if(!ret){
        return;
    }
    if(index != conf->confVar.CameraIndex){
        cameraTimer->stop();
        if(cameraStatus){
            camera->release();
        }
        delete camera;
        conf->confVar.CameraIndex = index;
        conf->saveCamera();
    }
    //重新初始化
    initCamera();
}

void MainWindow::initCamera(void){
    retInfo("initCamera");
    camera = new VideoCapture;
    cameraStatus = camera->open(conf->confVar.CameraIndex, CAP_DSHOW);
    retInfo(QString("cameraStatus %0 %1").arg(conf->confVar.CameraIndex).arg(cameraStatus));
    if(!cameraStatus){
        QMessageBox::warning(this,
                             "提示",
                             "未打开摄像设备，请检查");
        close();
    }else{
        getCameraResolution();
        setCameraResolution();
        cameraTimer->start(conf->confVar.CameraTime);
    }
}

// bool MainWindow::openCamera(int index){
//     return camera->open(index);
// }

// void MainWindow::closeCamera(void){
//     if(cameraStatus){
//         camera->release();
//     }
// }

void MainWindow::getCameraResolution(void){

    cameraWidth = camera->get(cv::CAP_PROP_FRAME_WIDTH);
    cameraHeight = camera->get(cv::CAP_PROP_FRAME_HEIGHT);
    retInfo(QString("getCameraResolution %0 %1").arg(cameraWidth).arg(cameraHeight));
}

void MainWindow::setCameraResolution(void){
    imageWidth=1280;
    imageHeight=720;

    switch(conf->confVar.ImageResolution){
        case 1:
            imageWidth = 1920;
            imageHeight = 1080;
            break;
        case 2:
            imageWidth = 2560;
            imageHeight = 1440;
            break;
        case 3:
            imageWidth = 3840;
            imageHeight = 2160;
            break;
    }
    if(cameraWidth != imageWidth || cameraHeight != imageHeight){
        bool ret1 = camera->set(cv::CAP_PROP_FRAME_WIDTH, imageWidth);
        bool ret2 = camera->set(cv::CAP_PROP_FRAME_HEIGHT, imageHeight);
        retInfo(QString("setCameraResolution %0 %1 %2 %3 %4").arg(conf->confVar.ImageResolution).arg(imageWidth).arg(imageHeight).arg(ret1).arg(ret2));
    }else{
        retInfo(QString("setCameraResolution %0 %1 %2").arg(conf->confVar.ImageResolution).arg(imageWidth).arg(imageHeight));
    }
}

void MainWindow::flushCameraTimer(void){
    if (!camera->isOpened()) {
        return;
    }

    camera->read(cameraFrame);

    if (cameraFrame.empty()){
        return;
    }
    // retInfo(QString("camera %0 %1 %2 ").arg(cameraFrame.cols).arg(cameraFrame.rows).arg(cameraFrame.channels()));
    Mat image = cameraFrame;
    if(cameraFrame.cols != imageWidth || cameraFrame.rows != imageHeight){
        imageWidth = cameraFrame.cols;
        imageHeight = cameraFrame.rows;
        bool bSetResolution = false;
        while(true){
            if(imageWidth == 1280 && imageHeight == 720){
                conf->confVar.ImageResolution = 0;
                bSetResolution = true;
                break;
            }
            if(imageWidth == 1920 && imageHeight == 1080){
                conf->confVar.ImageResolution = 1;
                bSetResolution = true;
                break;
            }
            if(imageWidth == 2560 && imageHeight == 1440){
                conf->confVar.ImageResolution = 2;
                bSetResolution = true;
                break;
            }
            if(imageWidth == 3840 && imageHeight == 2160){
                conf->confVar.ImageResolution = 3;
                bSetResolution = true;
                break;
            }
            //错误
            QMessageBox::warning(this,
                                 "提示",
                                 "摄像设备出错，退出");
            close();
            break;
        }
        if(bSetResolution){
            conf->saveConfig();
            setCameraResolution();
        }
        return;
    }

    if(conf->confVar.ImageGray){
        // 灰度
        cvtColor(image,image,COLOR_BGR2GRAY);
    }
    if(conf->confVar.ImageInverse){
        // 反向
        image = cvMatInverse(image);
    }
    if(conf->confVar.ImageHorizontal){
        flip(image,image,0);
    }
    if(conf->confVar.ImageVertical){
        flip(image,image,1);
    }
    switch(conf->confVar.ImageRotate){
        case 1:
            // -90
            transpose(image,image);
            flip(image,image,0);
            break;
        case 2:
            // +90
            transpose(image,image);
            flip(image,image,1);
            break;
        case 3:
            // 180
            flip(image,image,0);
            flip(image,image,1);
            break;
    }
    if(imageZoom&&imageZoomMutex->tryLock()){
        //放大
        int width=image.cols,height=image.rows;
        float zoom = float(10+imageZoom)/10.0;
        cv::Size dsize = cv::Size(image.cols*zoom,image.rows*zoom);
        cv::Mat src = cv::Mat(dsize, image.type());
        cv::resize(image, src, dsize);
        //位置
        cv::Rect box;
        //中心位置
        int x = imageBoxX;
        int y = imageBoxY;
        if(isFlushZoom){
            float fx = (float)imagePosX/(float)imageZoomWidth;
            float fy = (float)imagePosY/(float)imageZoomHeight;
            x = fx*(float)src.cols;
            y = fy*(float)src.rows;
            imageMoveX = width/2;
            imageMoveY = height/2;
            retInfo(QString("imageZoom flush %0 %1 pos %2 %3 , %4 %5, %6 %7, %8 %9")
                        .arg(src.cols).arg(src.rows).arg(imagePosX).arg(imagePosY)
                        .arg(x).arg(y).arg(imageZoomWidth).arg(imageZoomHeight)
                        .arg(getNumber(fx)).arg(getNumber(fy)));
        }
        x -= imageMoveX;
        y -= imageMoveY;
        box.x = x;
        box.y = y;
        if((box.x + width)>=src.cols){
            box.x = src.cols - width -1;
        }
        if(box.x < 0){
            box.x = 0;
        }
        if((box.y + height)>=src.rows){
            box.y = src.rows - height -1;
        }
        if(box.y < 0){
            box.y = 0;
        }
        box.width = width;
        box.height = height;
        // retInfo(QString("imageZoom %0 %1 %2 %3 win %4 %5 zoom %6 %7")
        //             .arg(box.x).arg(box.y).arg(box.width).arg(box.height)
        //             .arg(ui->labelImage->width()).arg(ui->labelImage->height())
        //             .arg(imageZoomWidth).arg(imageZoomHeight));
        imageBoxX = box.x;
        imageBoxY = box.y;
        image = src(box);
        // retInfo(QString("imageZoom %0 %1").arg(src.cols).arg(src.rows));
        imageZoomWidth = src.cols;
        imageZoomHeight = src.rows;
        imageMoveX = 0;
        imageMoveY = 0;
        isFlushZoom = false;
        imageZoomMutex->unlock();
    }else{
        imageZoomWidth = image.cols;
        imageZoomHeight = image.rows;
    }
    //颜色设置
    if(bImageColor || conf->confVar.ColorSetup){
        // retInfo(QString("imageColor Contrast=%0, Brightness=%1, Saturation=%2, Gamma=%3")
        //             .arg(conf->colorVar.ContrastValue)
        //             .arg(conf->colorVar.BrightnessValue)
        //             .arg(conf->colorVar.SaturationValue)
        //             .arg(conf->colorVar.GammaValue));
        image = cvMatContrastBrightness(conf->colorVar.ContrastValue, conf->colorVar.BrightnessValue, image);
        if(!conf->confVar.ImageGray){
            image = cvMatSaturation(conf->colorVar.SaturationValue, image);
        }
        image = cvMatGamma(conf->colorVar.GammaValue, image);
    }

    //局部放大
    if(bImageEnlarge){
        if(enlargeWindow->screenImageMutex->tryLock()){
            enlargeWindow->curEnlargeImage = image.clone();
            enlargeWindow->isFlush= true;
            enlargeWindow->screenImageMutex->unlock();
            emit SendEnclargeImage();
            // enlargeWindow->update();
        }
    }

    float scale=1.0;
    Mat img = matResize(image, ui->labelImage->width(), ui->labelImage->height(),scale);
    if(saveImageMutex->try_lock()){
        saveImage = img.clone();
        ui->labelImage->setGeometry(0, 0, img.cols, img.rows);
        ui->labelImage->setPixmap(cvMatToQPixmap(img));
        saveImageMutex->unlock();
    }
}

//鼠标按键
// void MainWindow::mousePressEvent(QMouseEvent *ev){
//     if(ev->buttons() & Qt::LeftButton){
//         if(!isLeftMousePressMove){
//             retInfo(QString("mouse press left (%0,%1)").arg(ev->pos().x()).arg(ev->pos().y()));
//             isLeftMousePressMove = true;
//         }
//         startPoint = ev->pos();
//     }
// }

//鼠标释放
// void MainWindow::mouseReleaseEvent(QMouseEvent *ev){
//     Q_UNUSED(ev);
//     if(isLeftMousePressMove){
//         retInfo(QString("mouse release left"));
//         isLeftMousePressMove = false;
//     }
// }

//鼠标移动
// void MainWindow::mouseMoveEvent(QMouseEvent *event){
//     retInfo(QString("MainWindow cur mouse move[%0,%1] %2 %3").arg(event->pos().x()).arg(event->pos().y()).arg(bImageEnlarge).arg(isLeftMousePressMove));
// }

//鼠标滚轴
// void MainWindow::wheelEvent(QWheelEvent *ev){
//     QPoint degrees = ev->angleDelta();
//     retInfo(QString("mainwindow mouse wheel x=%0,y=%1").arg(degrees.x()).arg(degrees.y()));
// }

//右键菜单
void MainWindow::on_MainWindow_customContextMenuRequested(const QPoint &pos)
{
    Q_UNUSED(pos);
    //创建菜单
    QString act,suffix;
    bool bCheck,bEnable;
    QMenu *pMenu = new QMenu(this);
    QList<QAction*> actions;
    actions.clear();
    for(int pos=0; pos< conf->actionList.count(); pos++)
    {
        // retInfo(QString("%0:%1,%2,%3").arg(pos).arg(conf->actionList[pos]).arg(conf->actionType[pos]).arg(conf->actionMark[pos]));
        suffix.clear();
        act = conf->actionList[pos];
        if(act == '0'){
            //间隔线
            actions.append(nullptr);
            pMenu->addSeparator();
            continue;
        }
        //判断项目
        bCheck = false;
        bEnable = (conf->actionType[pos]>0);

        switch(conf->actionMark[pos]){
            case 1:
                // 灰度
                if(conf->confVar.ImageGray){
                    bCheck = true;
                }
                break;
            case 3:
                // 分辨率-720P
                if(conf->confVar.ImageResolution == 0){
                    bCheck = true;
                }
                break;
            case 4:
                // 分辨率-1080P
                if(conf->confVar.ImageResolution == 1){
                    bCheck = true;
                }
                break;
            case 5:
                // 分辨率-2K
                if(conf->confVar.ImageResolution == 2){
                    bCheck = true;
                }
                break;
            case 6:
                // 分辨率-4K
                if(conf->confVar.ImageResolution == 3){
                    bCheck = true;
                }
                break;
            case 11:
                // 反向
                if(conf->confVar.ImageInverse){
                    bCheck = true;
                }
                break;
            case 12:
                // 颜色
                if(conf->confVar.ColorSetup){
                    bCheck = true;
                }
                break;
            case 13:
                // 设置
                bCheck = bImageColor;
                break;
            case 21:
                // 缩放锁定
                bCheck = conf->confVar.ImageZoom;
                if(bCheck){
                    suffix = QString("-%0").arg(imageZoom);
                }
                break;
            case 23:
                // 局部放大
                bCheck = bImageEnlarge;
                break;
            case 31:
                // 垂直翻转
                if(conf->confVar.ImageVertical){
                    bCheck = true;
                }
                break;
            case 32:
                // 水平翻转
                if(conf->confVar.ImageHorizontal){
                    bCheck = true;
                }
                break;
            case 41:
                // 不旋转
                if(conf->confVar.ImageRotate == 0){
                    bCheck = true;
                }
                break;
            case 42:
                // 旋转-90
                if(conf->confVar.ImageRotate == 1){
                    bCheck = true;
                }
                break;
            case 43:
                // 旋转+90
                if(conf->confVar.ImageRotate == 2){
                    bCheck = true;
                }
                break;
            case 44:
                // 旋转180
                if(conf->confVar.ImageRotate == 3){
                    bCheck = true;
                }
                break;
            case 81:
                // 快存
                break;
            case 82:
                // 保存
                break;
            case 90:
                // 切换
                break;
            case 99:
                // 关机
                break;
        }

        if(!suffix.isEmpty()){
            act += suffix;
        }
        actions.append(new QAction(act,this));
        actions[pos]->setData(conf->actionMark[pos]);
        pMenu->addAction(actions[pos]);
        actions[pos]->setCheckable(true);
        actions[pos]->setChecked(bCheck);
        actions[pos]->setEnabled(bEnable);

        connect(actions[pos], SIGNAL(triggered()), this, SLOT(onTaskBoxContextMenuEvent()));

    }
    pMenu->exec(cursor().pos());
    //释放内存
    for(int pos=0; pos< actions.count(); pos++)
    {
        if(actions[pos]!=nullptr)
        {
            delete actions[pos];
        }
    }
    delete pMenu;
}

void MainWindow::onTaskBoxContextMenuEvent(void){
    //this->sender()就是发信号者 QAction
    QAction *pEven = qobject_cast<QAction *>(this->sender());

    int mark = pEven->data().toInt();

    retInfo(QString("act %0").arg(mark));

    bool bSave = false;
    bool bSetResolution = false;
    switch(mark){
        case 1:
            // 灰度
            if(conf->confVar.ImageGray){
                conf->confVar.ImageGray = 0;
            }
            else {
                conf->confVar.ImageGray = 1;
            }
            bSave = true;
            break;
        case 3:
            // 分辨率-720P
            if(conf->confVar.ImageResolution != 0){
                conf->confVar.ImageResolution = 0;
                bSetResolution = true;
                bSave = true;
            }
            break;
        case 4:
            // 分辨率-1080P
            if(conf->confVar.ImageResolution != 1){
                conf->confVar.ImageResolution = 1;
                bSetResolution = true;
                bSave = true;
            }
            break;
        case 5:
            // 分辨率-2K
            if(conf->confVar.ImageResolution != 2){
                conf->confVar.ImageResolution = 2;
                bSetResolution = true;
                bSave = true;
            }
            break;
        case 6:
            // 分辨率-4K
            if(conf->confVar.ImageResolution != 3){
                conf->confVar.ImageResolution = 3;
                bSetResolution = true;
                bSave = true;
            }
            break;
        case 11:
            // 反向
            if(conf->confVar.ImageInverse){
                conf->confVar.ImageInverse = 0;
            }
            else {
                conf->confVar.ImageInverse = 1;
            }
            bSave = true;
            break;
        case 12:
            // 颜色
            if(conf->confVar.ColorSetup){
                conf->confVar.ColorSetup = 0;
            }
            else {
                conf->confVar.ColorSetup = 1;
            }
            bSave = true;
            break;
        case 13:
            // 设置
            onImageColor();
            break;
        case 21:
            // 缩放锁定
            if(conf->confVar.ImageZoom){
                conf->confVar.ImageZoom = 0;
            }
            else {
                conf->confVar.ImageZoom = 1;
            }
            bSave = true;
            break;
        case 23:
            // 局部放大
            onImageEnlarge();
            break;
        case 31:
            // 垂直翻转
            if(conf->confVar.ImageVertical){
                conf->confVar.ImageVertical = 0;
            }
            else {
                conf->confVar.ImageVertical = 1;
            }
            bSave = true;
            break;
        case 32:
            // 水平翻转
            if(conf->confVar.ImageHorizontal){
                conf->confVar.ImageHorizontal = 0;
            }
            else {
                conf->confVar.ImageHorizontal = 1;
            }
            bSave = true;
            break;
        case 41:
            // 不旋转
            if(conf->confVar.ImageRotate != 0){
                conf->confVar.ImageRotate = 0;
                bSave = true;
            }
            break;
        case 42:
            // 旋转-90
            if(conf->confVar.ImageRotate != 1){
                conf->confVar.ImageRotate = 1;
                bSave = true;
            }
            break;
        case 43:
            // 旋转+90
            if(conf->confVar.ImageRotate != 2){
                conf->confVar.ImageRotate = 2;
                bSave = true;
            }
            break;
        case 44:
            // 旋转180
            if(conf->confVar.ImageRotate != 3){
                conf->confVar.ImageRotate = 3;
                bSave = true;
            }
            break;
        case 81:
            // 快存
            saveCurImage0();
            break;
        case 82:
            // 保存
            saveCurImage1();
            break;
        case 90:
            // 切换
            changeCamera();
            return;
        case 99:
            // 关机
            if(bImageEnlarge){
                onImageEnlarge();
            }
            if(bImageColor){
                onImageColor();
            }
            close();
            return;
    }
    if(bSave){
        conf->saveConfig();
    }
    if(bSetResolution){
        setCameraResolution();
    }
}

//局部放大
void MainWindow::onImageEnlarge()
{
    bImageEnlarge = !bImageEnlarge;
    ui->labelImage->setEnlargeEnable(bImageEnlarge);
    if(bImageEnlarge){
        //打开
        enlargeWindow->setWin(conf->enlargeVar.WinX,conf->enlargeVar.WinY,conf->enlargeVar.WinWidth,conf->enlargeVar.WinHeight,size().width(),size().height());
        retInfo(QString("show enlarge %0 %1").arg(conf->enlargeVar.WinX).arg(conf->enlargeVar.WinY));
        enlargeWindow->show();
        enlargeWindow->setZoom(conf->enlargeVar.WinZoom);
        QObject::connect(ui->labelImage, SIGNAL(cursorMovePos(QPoint)), this, SLOT(recvCursorMovePos(QPoint)));
    }else{
        //关闭
        retInfo("hide enlarge");
        QObject::disconnect(ui->labelImage, SIGNAL(cursorMovePos(QPoint)), this, SLOT(recvCursorMovePos(QPoint)));
        enlargeWindow->hide();
    }
}

void MainWindow::recvEnclargeZoom(float zoom){
    conf->enlargeVar.WinZoom = zoom;
    conf->saveEnlarge();
    enlargeWindow->setZoom(conf->enlargeVar.WinZoom);
}

void MainWindow::recvEnclargeMove(int x,int y){
    conf->enlargeVar.WinX = x;
    conf->enlargeVar.WinY = y;
    conf->saveEnlarge();
}

void MainWindow::recvEnclargeSize(int w,int h){
    conf->enlargeVar.WinWidth = w;
    conf->enlargeVar.WinHeight = h;
    conf->saveEnlarge();
}

void MainWindow::recvCursorMovePos(QPoint pos){
    //有效
    int posX = pos.x();
    int posY = pos.y();
    emit SendEnclargeCursorPos(posX,posY);
}

void MainWindow::recvImageDoubleClicked(void){
    retInfo("recvImageDoubleClicked");
    emit SendEnclargeDouble();
}

void MainWindow::recvImageZoom(int zoom,int x,int y){
    int z = imageZoom;
    if(conf->confVar.ImageZoom){
        z += zoom/conf->confVar.MouseWheelStep;
    }else{
        z = 0;
    }
    if(z == imageZoom){
        return;
    }
    if(z<0){
        return;
    }
    if(z>=conf->confVar.ImageZoomMax){
        return;
    }
    //更改位置
    if(imageZoomMutex->tryLock()){
        imageZoom = z;
        imagePosX = x + imageBoxX;
        imagePosY = y + imageBoxY;
        isFlushZoom = true;
        ui->labelImage->zoom = imageZoom;
        // retInfo(QString("image zoom %0 %1 pos %2 %3").arg(zoom).arg(imageZoom).arg(x).arg(y));
        imageZoomMutex->unlock();
    }
}

void MainWindow::recvImageMove(int x,int y){
    if(imageZoomMutex->tryLock()){
        retInfo(QString("recv image move x=%0,y=%1 off:%2 %3")
                .arg(x).arg(y).arg(imageMoveX).arg(imageMoveY));
        imageMoveX += x;
        imageMoveY += y;
        imageZoomMutex->unlock();
    }
}

//颜色设置
void MainWindow::onImageColor()
{
    bImageColor = !bImageColor;
    if(bImageColor){
        colorWindow->setWin(conf->colorVar.WinX,conf->colorVar.WinY,conf->colorVar.WinWidth,conf->colorVar.WinHeight,size().width(),size().height());
        //打开
        colorWindow->setContrast(conf->colorVar.ContrastValue);
        colorWindow->setContrastLimit(conf->colorVar.ContrastMin,conf->colorVar.ContrastMax);
        colorWindow->setBrightness(conf->colorVar.BrightnessValue);
        colorWindow->setBrightnessLimit(conf->colorVar.BrightnessMin,conf->colorVar.BrightnessMax);
        retInfo(QString("imageGray %0").arg(conf->confVar.ImageGray));
        if(conf->confVar.ImageGray){
            colorWindow->setSaturation(-999);
        }else{
            colorWindow->setSaturation(conf->colorVar.SaturationValue);
            colorWindow->setSaturationLimit(conf->colorVar.SaturationMin,conf->colorVar.SaturationMax);
        }
        colorWindow->setGamma(conf->colorVar.GammaValue);
        colorWindow->setGammaLimit(conf->colorVar.GammaMin,conf->colorVar.GammaMax);
        colorWindow->show();
    }else{
        //关闭
        colorWindow->hide();
    }
}

void MainWindow::recvColorMove(int x,int y){
    conf->colorVar.WinX = x;
    conf->colorVar.WinY = y;
    conf->saveColor();
}

void MainWindow::recvColorSize(int w,int h){
    conf->colorVar.WinWidth = w;
    conf->colorVar.WinHeight = h;
    conf->saveColor();
}

void MainWindow::recvContrast(int value){
    retInfo(QString("recvContrast %0").arg(value));
    conf->colorVar.ContrastValue = value;
    conf->saveColor();
}

void MainWindow::recvBrightness(int value){
    retInfo(QString("recvBrightness %0").arg(value));
    conf->colorVar.BrightnessValue = value;
    conf->saveColor();
}
void MainWindow::recvSaturation(int value){
    retInfo(QString("recvSaturation %0").arg(value));
    conf->colorVar.SaturationValue = value;
    conf->saveColor();
}

void MainWindow::recvGamma(int value){
    retInfo(QString("recvGamma %0").arg(value));
    conf->colorVar.GammaValue = value;
    conf->saveColor();
}

void MainWindow::saveCurImage0(void){
    curDir = QString("%0").arg(conf->saveVar.CurDir);
    if(curDir.isEmpty()){
        curDir = getNewDir();
    }
    if(curDir.compare(conf->saveVar.CurDir)){
        qStrToCstr(curDir,conf->saveVar.CurDir,sizeof(conf->saveVar.CurDir));
        conf->saveSave();
    }
    QString path = QString("%0/%1").arg(conf->saveVar.SavePath).arg(curDir);
    curFileNo = getDirNums(path) + 1;
    if(saveImageMutex->try_lock()){
        QString name(QString("%0/%1.jpg").arg(path).arg(curFileNo,5,10,QLatin1Char('0')));
        cvMatSave(saveImage,name);
        saveImageMutex->unlock();
    }
}

void MainWindow::recvSaveCurDir(QString dir){
    retInfo(QString("recvSaveCurDir %0").arg(dir));
    curDir = dir;
    if(curDir.compare(conf->saveVar.CurDir)){
        qStrToCstr(curDir,conf->saveVar.CurDir,sizeof(conf->saveVar.CurDir));
        conf->saveSave();
    }
}

void MainWindow::saveCurImage1(void){
    saveUI *save = new saveUI(nullptr, conf->saveVar.SavePath, conf->saveVar.CurDir);
    save->setWindowModality(Qt::ApplicationModal);
    QObject::connect(save, SIGNAL(sendExit(void)), this, SLOT(saveCurImage0(void)));
    QObject::connect(save, SIGNAL(sendCurPath(QString)), this, SLOT(recvSaveCurDir(QString)));
    save->show();
}
